//
//  XS_PlanCalendarCell.swift
//  XueDao
//
//  Created by Mac on 2020/12/15.
//

import UIKit
import CVCalendar

protocol XS_PlanCalendarCellDelegate {
    func calendarCell(calendarCell : XS_PlanCalendarCell, didSelectDate:CVCalendarDayView)
}

class XS_PlanCalendarCell: UICollectionViewCell {
    
    var delegate : XS_PlanCalendarCellDelegate?
    
    private var lineView : UIView?
    //星期菜单栏
    private var menuView: CVCalendarMenuView!
    
    //日历主视图
    private var calendarView: CVCalendarView!
    
    var currentCalendar: Calendar!
    
    var _dataArray : [XS_WeekItemModel]!
    public var dataArray : [XS_WeekItemModel]!{
        set{
            _dataArray = newValue
            if !self.contentView.subviews.contains(calendarView) {
                //星期菜单栏代理
                menuView.menuViewDelegate = self
                //日历代理
                calendarView.calendarDelegate = self
                //日历样式代理
                calendarView.calendarAppearanceDelegate = self
                
                contentView.addSubview(menuView)
                contentView.addSubview(calendarView)
                
                layoutSubviews()
            }
            calendarView.contentController.refreshPresentedMonth()
        }
        get{
            return _dataArray
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        lineView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: .screenWidth - 32, height: 4))
        lineView?.backgroundColor = #colorLiteral(red: 0.1716121137, green: 0.7779735923, blue: 0.675088048, alpha: 1)
        addSubview(lineView!)
        
        currentCalendar = Calendar.init(identifier: .gregorian)
        
        //初始化星期菜单栏
        menuView = CVCalendarMenuView(frame: CGRect(x:0, y:0, width:.screenWidth - 32, height:40))
        //初始化日历主视图
        calendarView = CVCalendarView(frame: CGRect(x:0, y:25, width:.screenWidth - 32, height:70))

        let layerColor = UIColor(red: 0.18, green: 0.20, blue: 0.20, alpha: 0.06).cgColor
        contentView.layer.borderWidth = 1
        contentView.layer.borderColor = layerColor

        NotificationCenter.default.addObserver(self, selector: #selector(showToday), name: NSNotification.Name(rawValue: "ShowTodayNoti"), object: nil)
    }
    
    @objc func showToday(){
        calendarView.toggleViewWithDate(Date())
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //更新日历frame
        menuView.commitMenuViewUpdate()
        calendarView.commitCalendarViewUpdate()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension XS_PlanCalendarCell: CVCalendarViewDelegate,CVCalendarMenuViewDelegate, CVCalendarViewAppearanceDelegate {
    //视图模式
    func presentationMode() -> CalendarMode {
        //使用周视图
        return .weekView
    }
    
    //每周的第一天
    func firstWeekday() -> Weekday {
        //从星期一开始
        return .monday
    }
    
    func presentedDateUpdated(_ date: CVDate) {
        //导航栏显示当前日历的年月
        
    }
    
    //每个日期上面是否添加横线(连在一起就形成每行的分隔线)
    func topMarker(shouldDisplayOnDayView dayView: CVCalendarDayView) -> Bool {
        return false
    }
    
    //切换周的时候日历是否自动选择某一天（本周为今天，其它周为第一天）
    func shouldAutoSelectDayOnWeekChange() -> Bool {
        return false
    }
    
    //日期选择响应
    func didSelectDayView(_ dayView: CVCalendarDayView, animationDidFinish: Bool) {
        //获取日期
        if delegate != nil {
            delegate?.calendarCell(calendarCell: self, didSelectDate: dayView)
        }
    }
    
    func dotMarker(shouldShowOnDayView dayView: DayView) -> Bool {
        return true
    }
    
    func dotMarker(colorOnDayView dayView: DayView) -> [UIColor] {
        for model in dataArray {
            if model.day == dayView.date.day {
                if model.state == "yes" {
                    if model.dateState == "before" {
                        return [#colorLiteral(red: 0.768627451, green: 0.7764705882, blue: 0.8039215686, alpha: 1)]
                    }
                    return [#colorLiteral(red: 0.1716121137, green: 0.7779735923, blue: 0.675088048, alpha: 1)]
                }else if model.state == "no"{
                    return []
                }
                return []
            }
        }
        return []
    }
    
    //星期栏文字显示类型
    func weekdaySymbolType() -> WeekdaySymbolType {
        //正常
        return .veryShort
    }
    
    //星期栏文字字体
    func dayOfWeekFont() -> UIFont {
        return .systemFont(ofSize: 14, weight: .medium)
    }
    
    //文字字体设置
    func dayLabelFont(by weekDay: Weekday, status: CVStatus, present: CVPresent) -> UIFont {
        return UIFont.systemFont(ofSize: 14)
    }
    
    //文字背景色设置
    func dayLabelBackgroundColor(by weekDay: Weekday, status: CVStatus, present: CVPresent) -> UIColor? {
        if status == .selected {
            return #colorLiteral(red: 0.159416765, green: 0.7414153218, blue: 0.6134808064, alpha: 1)
        }
        return .white
    }
    
    //标记点的偏移量
    func dotMarker(moveOffsetOnDayView dayView: DayView) -> CGFloat {
        return 28
    }
    
    //标记点的尺寸大小
    func dotMarker(sizeOnDayView dayView: DayView) -> CGFloat {
        return 16
    }
    
    func shouldShowWeekdaysOut() -> Bool {
        return true
    }
    
    func disableScrollingBeyondDate() -> Date {
        return Date()
    }
    
    func disableScrollingBeforeDate() -> Date {
        return Date()
    }
}
